package cn.fl.institution.controller;

import cmm.mid.core.framework.constant.tripartite.FileConstants;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.dto.MessageDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.*;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.api.IPrjCstAccInfoApiService;
import cn.fl.project.condition.PrjCstAccInfoQueryCondition;
import cn.fl.project.condition.PrjFileInfoQueryCondition;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjFileInfoService;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjCstAccInfoVO;
import cn.fl.project.vo.PrjFileInfoVO;
import cn.fl.system.api.ISysInsIvkValApiService;
import cn.fl.system.dto.SysInsIvkValDTO;
import cn.fl.upload.constant.UploadConstant;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.SysInsInterfaceConstant;
import fl.inf.facade.api.ITransLogApi;
import fl.inf.facade.condition.InfTransLogQueryCondition;
import fl.inf.facade.dto.InfTransLogDTO;
import fl.spdbank.facade.api.ISpdbankApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.util.List;
import java.util.*;

/**
 * @Description: 项目基础信息表 控制层 浦发银行
 */
@Controller
@RequestMapping("/institution/spdbank")
public class PrjBscInfoInsSPDController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PrjBscInfoInsSPDController.class);

    @Autowired
    private IPrjBscInfoService prjBscInfoService; // 项目基础信息表

	@Autowired
	private ISysInsIvkValApiService sysInsIvkValApiService; // 资管机构承接调用结果

	@Autowired
	private IPrjCstAccInfoApiService prjCstAccInfoApiService; // 项目客户账户信息表

	@Autowired
    private IPrjFileInfoService prjFileInfoService;

	@Autowired
	private ISpdbankApi spdbankApi; // 浦发银行接口规范

	@Autowired
	private ICstNpBscInfoService cstNpBscInfoService; // 自然人客户基础信息表

	@Autowired
	private ICstBscInfoService cstBscInfoService; // 客户基础信息
	
	@Autowired
	private ITransLogApi transLogApi;
	
	/**
	 * 保存 项目客户信息，为浦发银行开户作准备。
	 */
	@RequestMapping(value = "/save.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object save(Long prjId, HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "保存成功!");
		BaseLogUtils.info(logger, "save", "项目浦发信息保存 ： ");
//
		try {
			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(prjId);
			BaseLogUtils.info(logger, "save", "项目浦发信息保存 ： prjBscInfoVO = " + JSONObject.toJSONString(prjBscInfoVO));

			PrjCstAccInfoVO prjCstAccInfoVO = prjBscInfoVO.getPrjCstAccInfoVO();
			BaseLogUtils.info(logger, "save", "项目浦发信息保存 ： prjCstAccInfoVO = " + JSONObject.toJSONString(prjCstAccInfoVO));

			// 查找开户接口的调用结果
//			SysInsIvkValDTO sysInsIvkValDTO = sysInsIvkValApiService.getSysInsIvkValDTO(prjBscInfoVO.getId(), prjBscInfoVO.getFundRasInsId(),
//					SysInsInterfaceConstant.InsTypeIntfConstant.SPDBANK_TYPE_CREATE_ACCOUNT);
//			BaseLogUtils.info(logger, "save", "项目浦发信息保存 ： sysInsIvkValDTO = " + JSONObject.toJSONString(sysInsIvkValDTO));
			
			boolean createAccountSucc = false;
			if(prjCstAccInfoVO != null) {
				InfTransLogQueryCondition infTransLogQueryCondition = new InfTransLogQueryCondition();
				infTransLogQueryCondition.setPrjId(prjCstAccInfoVO.getPrjId());
				infTransLogQueryCondition.setCstId(prjCstAccInfoVO.getCstId());
				infTransLogQueryCondition.setFundRasInsId(prjBscInfoVO.getFundRasInsId());
				infTransLogQueryCondition.setAction(SysInsInterfaceConstant.InsTypeIntfConstant.SPDBANK_TYPE_QUERY_ACCOUNT);
				List<InfTransLogDTO> infTransLogDTOList = transLogApi.findTransLogs(infTransLogQueryCondition);
				InfTransLogDTO infTransLogDTO = null;
				if(CollectionUtils.isNotEmpty(infTransLogDTOList)) {
					infTransLogDTO = infTransLogDTOList.get(0);
				}
				BaseLogUtils.info(logger, "save", "项目浦发信息保存 ： infTransLogDTO = " 
						+ (infTransLogDTO==null?"null":JSONObject.toJSONString(infTransLogDTO)));
				createAccountSucc = infTransLogDTO != null 
						&& ("0000".equals(infTransLogDTO.getRespCode()) || "1000".equals(infTransLogDTO.getRespCode()));
			}

			// 如果 开户接口的调用信息 不存在或者是失败，那么项目客户信息可以进行修改
			if(!createAccountSucc) {
				PrjCstAccInfoQueryCondition prjCstAccInfoQC = this.getRequestParamForPrjCstAccInfo(request, prjCstAccInfoVO);
				BaseLogUtils.info(logger, "save", "项目浦发信息保存 ： " + JSONObject.toJSONString(prjCstAccInfoQC));

				// 保存项目客户信息，包含四要素验证
                prjCstAccInfoQC.setInsCode(PrjBscInfoConstant.PRJ_INS_CODE_SPD);
				DataResultDTO resultDTO = this.prjCstAccInfoApiService.modifyPrjAccInfo(prjCstAccInfoQC);
				BaseLogUtils.info(logger, "save", "项目浦发信息保存.resultDTO ： " + JSONObject.toJSONString(resultDTO));

				String chkRstCd = null, rstDesc = null;
				if(resultDTO != null && StringUtils.isNotEmpty(resultDTO.getCode())
						&& Integer.valueOf(resultDTO.getCode()) != BaseConstant.IS_YESNO_YES) {
					chkRstCd = resultDTO.getCode();
					rstDesc = resultDTO.getInfo();
				}
				else if(resultDTO != null && resultDTO.getData() != null) {
					@SuppressWarnings("unchecked")
					Map<String, String> ckResMap = (Map<String, String>) resultDTO.getData();
					chkRstCd = ckResMap.get("chkRstCd");
					rstDesc = ckResMap.get("rstDesc");
				}

				// 保存月收入
				if(null != prjBscInfoVO.getCstId()) {
					CstNpBscInfoVO cstNpBscInfoVO = this.cstNpBscInfoService.selectById(prjBscInfoVO.getCstId());
					String monIncmYuanStr = request.getParameter("monIncmYuan");
					cstNpBscInfoVO.setYrInc(DecimalUtils.convertYuanToFen(DecimalUtils.mul(Double.valueOf(monIncmYuanStr), 12D)));
					this.cstNpBscInfoService.modify(cstNpBscInfoVO);
				}

				if(chkRstCd == null || Integer.valueOf(chkRstCd) != BaseConstant.IS_YESNO_YES) {
					if(StringUtils.isNotEmpty(rstDesc)) {
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo(rstDesc);
					}
					else {
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo(resultDTO.getInfo());
					}
				}
			}
		}
		catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "save", "项目浦发信息保存失败 " + ex.getMessage());
			ex.printStackTrace();
		}
		catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("保存失败!");
			BaseLogUtils.error(logger, "save", "项目浦发信息保存失败 " + ex.getMessage());
			ex.printStackTrace();
		}

		return result;
	}

	private PrjCstAccInfoQueryCondition getRequestParamForPrjCstAccInfo(HttpServletRequest request, PrjCstAccInfoVO prjCstAccInfoVO) throws BaseException {
		PrjCstAccInfoQueryCondition prjCstAccInfoQC = new PrjCstAccInfoQueryCondition();

		//获取参数
		Long id = prjCstAccInfoVO == null ? null : prjCstAccInfoVO.getId();
		String prjId = request.getParameter("prjId"); // 项目ID
		String cstId = request.getParameter("cstId"); // 客户ID
		String cstNm = request.getParameter("cstNm");//客户名称
		String mp = request.getParameter("mp");//客户手机号
//		String certNo = request.getParameter("certNo");//证件号码
		String accNo = request.getParameter("accNo");//银行账号
		String certTypCd = DictionaryConstant.CODE_CERT_TYP_CD_0;//证件类型（CST0005）
		String bkNm = request.getParameter("bkNm");//开户银行名称
		String bkNo = request.getParameter("bkNo");//开户行号
//		String chkRstCd = request.getParameter("chkRstCd");//校验结果 1.校验通过 2.校验不通过3.校验失败
		String fundRasInsId = request.getParameter("fundRasInsId");

		// 身份证号码
		String certNo = null;
		CstBscInfoVO cstBscInfoVO = this.cstBscInfoService.selectDetailById(Long.valueOf(cstId));
		if (DictionaryConstant.CODE_CERT_TYP_CD_0.equals(cstBscInfoVO.getCertTypCd())) {
			certNo = cstBscInfoVO.getCertNo(); // 证件号码
		}

		prjCstAccInfoQC.setPrjId(Long.valueOf(prjId));
		prjCstAccInfoQC.setCstId(Long.valueOf(cstId));
		prjCstAccInfoQC.setCstNm(cstNm);//客户名称
		prjCstAccInfoQC.setAccNm(cstNm);
		prjCstAccInfoQC.setBkCd(bkNo);
 		prjCstAccInfoQC.setMp(mp);//客户手机号
 		prjCstAccInfoQC.setCertNo(certNo);//证件号码
 		prjCstAccInfoQC.setAccNo(accNo);//银行账号
 		prjCstAccInfoQC.setCertTypCd(certTypCd);//证件类型（CST0005）
 		prjCstAccInfoQC.setBkNm(bkNm);//开户银行名称
 		prjCstAccInfoQC.setBkNo(bkNo);//开户行号
 		if(fundRasInsId != null) {
 			prjCstAccInfoQC.setInsId(Long.valueOf(fundRasInsId));
 		}

		if (id != null) {
			prjCstAccInfoQC.setId(id);
		}

		return prjCstAccInfoQC;
	}

	/**
	 * 调用准入验证前确认资料信息
	 * @param cstId 客户ID
	 * @param cstMgrOrgCd 客户经理组织架构代码
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	@RequestMapping(value = "/toConfirmFileForCreditPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public Object toConfirmFileForCreditPage(Long prjId, HttpServletRequest request, HttpServletResponse response) throws BaseException {
		ModelAndView view = new ModelAndView("/institution/spdbank/confirmFileForCredit");
    	BaseLogUtils.info(logger, "toConfirmFileForCreditPage", "prjId >>>>>> " + prjId);

		PrjBscInfoVO prjBsc = this.prjBscInfoService.selectById(prjId);

    	PrjFileInfoQueryCondition qc = new PrjFileInfoQueryCondition();
    	qc.setCstId(prjBsc.getCstId());
    	qc.setCstMgrOrgCd(prjBsc.getCstMgrOrgCd());
    	BaseLogUtils.info(logger, "toConfirmFileForCreditPage", "PrjFileInfoQueryCondition >>>>>> " + qc);
    	List<PrjFileInfoVO> list = this.prjFileInfoService.searchCstFileInfoList(qc);

		String idCardPositiveFilePath = null; // 身份证正面 文件路径
		String idCardPositiveFileName = null; // 身份证正面 文件名称
		String idCardObverseFilePath = null; // 身份证反面 文件路径
		String idCardObverseFileName = null; // 身份证反面 文件名称
		for(PrjFileInfoVO prjFileInfoVO : list) {
			if(StringUtils.isNotEmpty(prjFileInfoVO.getFilePath())
					&& FileConstants.FILE_ID_CARD_POSITIVE_CD.equals(prjFileInfoVO.getCfgDtlCd())) { // 身份证正面标识-自然人资料-CST004
				idCardPositiveFilePath = prjFileInfoVO.getFilePath();
				idCardPositiveFileName = prjFileInfoVO.getFileNm();
			}
			if(StringUtils.isNotEmpty(prjFileInfoVO.getFilePath())
					&& FileConstants.FILE_ID_CARD_OBVERSE_CD.equals(prjFileInfoVO.getCfgDtlCd())) { // 身份证反面标识-自然人资料-CST004
				idCardObverseFilePath = prjFileInfoVO.getFilePath();
				idCardObverseFileName = prjFileInfoVO.getFileNm();
			}
		}

		String positiveError = null;
		if(StringUtils.isNotEmpty(idCardPositiveFilePath)) {
			positiveError = this.verifyImage(idCardPositiveFilePath);
		}
		String obverseError = null;
		if(StringUtils.isNotEmpty(idCardObverseFilePath)) {
			obverseError = this.verifyImage(idCardObverseFilePath);
		}
		BaseLogUtils.info(logger, "toConfirmFileForCreditPage", "idCardPositiveFilePath >>>>>> " + idCardPositiveFilePath);
		BaseLogUtils.info(logger, "toConfirmFileForCreditPage", "idCardPositiveFileName >>>>>> " + idCardPositiveFileName);
		BaseLogUtils.info(logger, "toConfirmFileForCreditPage", "idCardObverseFilePath >>>>>> " + idCardObverseFilePath);
		BaseLogUtils.info(logger, "toConfirmFileForCreditPage", "idCardObverseFileName >>>>>> " + idCardObverseFileName);

		view.addObject("prjId", prjId);
		view.addObject("cstNm", prjBsc.getPrjCstAccInfoVO().getCstNm());
		view.addObject("mp", prjBsc.getPrjCstAccInfoVO().getMp());
		view.addObject("certNo", prjBsc.getPrjCstAccInfoVO().getCertNo());
		view.addObject("positiveError", positiveError);
		view.addObject("obverseError", obverseError);
		view.addObject("idCardPositiveFilePath", idCardPositiveFilePath);
		view.addObject("idCardPositiveFileName", idCardPositiveFileName);
		view.addObject("idCardObverseFilePath", idCardObverseFilePath);
		view.addObject("idCardObverseFileName", idCardObverseFileName);

		return view;
	}

	public String verifyImage(String imagePath) {
		if(imagePath.startsWith("https:")) {
			for(int i=0; i<3; i++) {
				if(imagePath.indexOf('/') >= 0) {
					imagePath = imagePath.substring(imagePath.indexOf('/')+1, imagePath.length());
				}
			}
		}
		if(imagePath.startsWith("http:")) {
			for(int i=0; i<3; i++) {
				if(imagePath.indexOf('/') >= 0) {
					imagePath = imagePath.substring(imagePath.indexOf('/')+1, imagePath.length());
				}
			}
		}
		imagePath = PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_ROOT_PATH) + "/" + imagePath;

		File file = new File(imagePath);
		String error = null, postfix = null;
		if(file.exists() && file.isFile()) {

			// 校验文件大小
			long filesize = file.length();
			if(filesize >  1024*512) {
				error = "超过文件大小限制("+FileUtils.getLocalFileSize(imagePath)+")";
			}

			// 校验文件类型
			if (imagePath.lastIndexOf('.') > 0 && imagePath.lastIndexOf('.') <= imagePath.length() - 1) {
				postfix = imagePath.substring(imagePath.lastIndexOf('.') + 1, imagePath.length());
			} else {
				postfix = imagePath.length() > 4 ? imagePath.substring(imagePath.length() - 3, imagePath.length()) : null;
			}
			if(!"JPG".equals(postfix.toUpperCase()) && !"PNG".equals(postfix.toUpperCase())) {
				error = (error==null?"":error) + "文件类型("+postfix+")不正确;";
			}

			// 校验图片分辨率
			try {
				Image src = javax.imageio.ImageIO.read(file);
				int width = src.getWidth(null);
				int height = src.getHeight(null);
				if(width < 800 || height < 480) {
					error = (error==null?"":error) + "图片分辨率过小("+width+"*"+height+");";
				}
			}
			catch(Exception ex) {
				ex.printStackTrace();
				error = (error==null?"":error) + "图片分辨率解析失败;";
			}
		}
		else {
			error = "文件不存在;";
		}
		BaseLogUtils.info(logger, "verifyImage", "verifyImage >>>>>> " + (imagePath) + " " + error);
		return error;
	}

	/**
	 * 准入验证
	 * @param prjId 项目ID
	 * @param fundRasInsId 金融机构ID
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	@RequestMapping(value = "/submitCredit.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object submitCredit(Long prjId, Long fundRasInsId, Long cstId,
			HttpServletRequest request, HttpServletResponse response) throws BaseException {
    	BaseLogUtils.info(logger, "submitCredit", "prjId >>>>>> " + prjId);
    	BaseLogUtils.info(logger, "submitCredit", "fundRasInsId >>>>>> " + fundRasInsId);
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "准入验证成功!");
		SysInsIvkValDTO sysInsIvkValDTO = null; // 准入校验接口调用结果
		boolean success = false;
		String respCode = null;
		String respDesc = null;
		try {
			sysInsIvkValDTO = sysInsIvkValApiService.getSysInsIvkValDTO(prjId, fundRasInsId,
					SysInsInterfaceConstant.InsTypeIntfConstant.SPDBANK_TYPE_SUBMIT_CREDIT);
			if(null == sysInsIvkValDTO) {
				sysInsIvkValDTO = new SysInsIvkValDTO();
			}
	    	BaseLogUtils.info(logger, "submitCredit", "sysInsIvkValDTO >>>>>> " + JSONObject.toJSONString(sysInsIvkValDTO));

			success = sysInsIvkValDTO != null && sysInsIvkValDTO.isSuccess();
			if(!success) { // 如果之前是不成功状态，那么重新调用接口
				sysInsIvkValDTO.setPrjId(prjId);
				sysInsIvkValDTO.setCstId(cstId);
				sysInsIvkValDTO.setInsId(fundRasInsId);
				sysInsIvkValDTO.setTypeCd(SysInsInterfaceConstant.InsTypeIntfConstant.SPDBANK_TYPE_SUBMIT_CREDIT);
				if(null == sysInsIvkValDTO.getId()) {
					Long sysInsIvkValId = sysInsIvkValApiService.add(sysInsIvkValDTO);
					sysInsIvkValDTO.setId(sysInsIvkValId);
			    	BaseLogUtils.info(logger, "submitCredit", "sysInsIvkValApiService.add >>>>>> " + JSONObject.toJSONString(sysInsIvkValDTO));
				}
				else {
					sysInsIvkValApiService.modify(sysInsIvkValDTO);
			    	BaseLogUtils.info(logger, "submitCredit", "sysInsIvkValApiService.modify >>>>>> " + JSONObject.toJSONString(sysInsIvkValDTO));
				}

//				success = true;
				MessageDTO<String> messageDTO = spdbankApi.submitCredit(fundRasInsId, prjId);
				result.setData(messageDTO);
				if(null == messageDTO || !messageDTO.isSuccess()) {
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("准入验证失败");
				}
				if(null != messageDTO) {
					respCode = messageDTO.getCode();
					respDesc = messageDTO.getMessage();
					success = messageDTO.isSuccess();
				}
				if(messageDTO.isSuccess()) {
					respCode = SysInsInterfaceConstant.DEFAULT_RESULT_CODE_SUCC;
					BaseLogUtils.info(logger, "addApplyLoanAgain", "=调用浦发网贷个人客户必要信息查询Start=prjId:"+prjId+",fId:"+fundRasInsId);
					MessageDTO<String> verifyIdentificationDTO = spdbankApi.verifyIdentification(fundRasInsId, prjId);
					BaseLogUtils.info(logger, "addApplyLoanAgain", "=调用浦发网贷个人客户必要信息查询End=prjId:"+prjId+",verifyIdentificationDTO:"+ JSON.toJSONString(verifyIdentificationDTO));
					if(null==verifyIdentificationDTO || !verifyIdentificationDTO.isSuccess()){
						success = false;
						respDesc = "浦发网贷个人客户必要信息查询失败!";
						if(null != verifyIdentificationDTO) {
							respCode = verifyIdentificationDTO.getCode();
							if(StringUtils.isNotEmpty(verifyIdentificationDTO.getMessage())){
								respDesc = messageDTO.getMessage();
							}
						}
					}
				}
			}
		}
		catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger,"add","准入校验失败"+ ex.getMessage());
			ex.printStackTrace();
		}
		catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("准入校验失败!");
			BaseLogUtils.error(logger,"add","准入校验失败"+ ex.getMessage());
			ex.printStackTrace();
		}
		finally {

			// 更新调用结果
			try {
				if(null != sysInsIvkValDTO && null != sysInsIvkValDTO.getId() && !sysInsIvkValDTO.isSuccess()) {
					if(StringUtils.isNotEmpty(respCode)) {
						sysInsIvkValDTO.setRespCode(respCode);
					}
					else {
						if(success) {
							sysInsIvkValDTO.setRespCode(SysInsInterfaceConstant.DEFAULT_RESULT_CODE_SUCC);
						}
						else {
							sysInsIvkValDTO.setRespCode(SysInsInterfaceConstant.DEFAULT_RESULT_CODE_FAILED);
						}
					}
					if(StringUtils.isNotEmpty(respDesc)) {
						sysInsIvkValDTO.setRespDesc(respDesc);
					}
					else {
						if(success) {
							sysInsIvkValDTO.setRespDesc(SysInsInterfaceConstant.DEFAULT_RESULT_DESC_SUCC);
						}
						else {
							sysInsIvkValDTO.setRespDesc(SysInsInterfaceConstant.DEFAULT_RESULT_DESC_FAILED);
						}
					}
					sysInsIvkValApiService.modify(sysInsIvkValDTO);
			    	BaseLogUtils.info(logger, "submitCredit", "sysInsIvkValApiService.modify result >>>>>> " + JSONObject.toJSONString(sysInsIvkValDTO));
				}
			}
			catch(Exception ex) {
				BaseLogUtils.error(logger, "submitCredit", "准入校验失败.保存调用记录异常" + ex.getMessage());
				ex.printStackTrace();
			}
			if(!success) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("准入校验失败" + (StringUtils.isNotEmpty(respDesc) ? "，"+respDesc : "!"));
			}
		}

		return result;
	}

	/**
	 * 获取验证码提交（图文信息上传、随机密码获取）
	 * @param prjId 项目ID
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	@RequestMapping(value = "/getVerifyCode.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object getVerifyCode(Long prjId, Long fundRasInsId, Long cstId,
			HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取验证码提交成功!");

		Map<String, String> data = new HashMap<>();
		data.put("uploadFileSucc", "false");
		data.put("getVerifyCodeSucc", "false");
		result.setData(data);

		SysInsIvkValDTO sysInsIvkValDTO = null; // 图文信息上传调用结果
		boolean success = false;
		String respCode = null;
		String respDesc = null;
		try {
			sysInsIvkValDTO = sysInsIvkValApiService.getSysInsIvkValDTO(prjId, fundRasInsId,
					SysInsInterfaceConstant.InsTypeIntfConstant.SPDBANK_TYPE_UPLOAD_FILE);
			if(null == sysInsIvkValDTO) {
				sysInsIvkValDTO = new SysInsIvkValDTO();
			}
			success = sysInsIvkValDTO != null && sysInsIvkValDTO.isSuccess();
			if(!success) { // 如果之前是不成功状态，那么重新调用接口
				sysInsIvkValDTO.setPrjId(prjId);
				sysInsIvkValDTO.setCstId(cstId);
				sysInsIvkValDTO.setInsId(fundRasInsId);
				sysInsIvkValDTO.setTypeCd(SysInsInterfaceConstant.InsTypeIntfConstant.SPDBANK_TYPE_UPLOAD_FILE);
				if(null == sysInsIvkValDTO.getId()) {
					Long sysInsIvkValId = sysInsIvkValApiService.add(sysInsIvkValDTO);
					sysInsIvkValDTO.setId(sysInsIvkValId);
				}
				else {
					sysInsIvkValApiService.modify(sysInsIvkValDTO);
				}

//				success = true;
				MessageDTO<String> messageDTO = spdbankApi.uploadFile(fundRasInsId, prjId);
				if(null == messageDTO || !messageDTO.isSuccess()) {
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("图文信息上传失败");
				}
				if(null != messageDTO) {
					respCode = messageDTO.getCode();
					respDesc = messageDTO.getMessage();
					success = messageDTO.isSuccess();
				}
				if(success) {
					respCode = SysInsInterfaceConstant.DEFAULT_RESULT_CODE_SUCC;
					data.put("uploadFileSucc", "true");
				}
			}
		}
		catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "getVerifyCode", "图文信息上传失败" + ex.getMessage());
			ex.printStackTrace();
		}
		catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取验证码提交.图文信息上传.失败!");
			BaseLogUtils.error(logger, "getVerifyCode", "图文信息上传失败" + ex.getMessage());
			ex.printStackTrace();
		}
		finally {

			// 更新调用结果
			try {
				if(null != sysInsIvkValDTO && null != sysInsIvkValDTO.getId() && !sysInsIvkValDTO.isSuccess()) {
					if(StringUtils.isNotEmpty(respCode)) {
						sysInsIvkValDTO.setRespCode(respCode);
					}
					else {
						if(success) {
							sysInsIvkValDTO.setRespCode(SysInsInterfaceConstant.DEFAULT_RESULT_CODE_SUCC);
						}
						else {
							sysInsIvkValDTO.setRespCode(SysInsInterfaceConstant.DEFAULT_RESULT_CODE_FAILED);
						}
					}
					if(StringUtils.isNotEmpty(respDesc)) {
						sysInsIvkValDTO.setRespDesc(respDesc);
					}
					else {
						if(success) {
							sysInsIvkValDTO.setRespDesc(SysInsInterfaceConstant.DEFAULT_RESULT_DESC_SUCC);
						}
						else {
							sysInsIvkValDTO.setRespDesc(SysInsInterfaceConstant.DEFAULT_RESULT_DESC_FAILED);
						}
					}
					sysInsIvkValApiService.modify(sysInsIvkValDTO);
			    	BaseLogUtils.info(logger, "getVerifyCode", "sysInsIvkValApiService.modify result >>>>>> " + JSONObject.toJSONString(sysInsIvkValDTO));
				}
			}
			catch(Exception ex) {
				BaseLogUtils.error(logger, "getVerifyCode", "获取验证码提交.图文信息上传.保存调用记录异常" + ex.getMessage());
			}
			if(!success) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("获取验证码提交.图文信息上传.失败" + (StringUtils.isNotEmpty(respDesc) ? "，"+respDesc : "!"));
			}
		}

		// 如果图文信息上传失败直接返回页面
		if(!success) {
			return result;
		}

		sysInsIvkValDTO = null; // 随机密码获取接口调用结果
		success = false;
		respCode = null;
		respDesc = null;
		try {
			sysInsIvkValDTO = sysInsIvkValApiService.getSysInsIvkValDTO(prjId, fundRasInsId,
					SysInsInterfaceConstant.InsTypeIntfConstant.SPDBANK_TYPE_CREATE_ACCOUNT);
			if(null == sysInsIvkValDTO) {
				sysInsIvkValDTO = new SysInsIvkValDTO();
			}
			success = sysInsIvkValDTO != null && sysInsIvkValDTO.isSuccess();
			if(!success) { // 如果之前是不成功状态，那么重新调用接口
				sysInsIvkValDTO.setPrjId(prjId);
				sysInsIvkValDTO.setCstId(cstId);
				sysInsIvkValDTO.setInsId(fundRasInsId);
				sysInsIvkValDTO.setTypeCd(SysInsInterfaceConstant.InsTypeIntfConstant.SPDBANK_TYPE_GET_VERIFY_CODE);
				if(null == sysInsIvkValDTO.getId()) {
					Long sysInsIvkValId = sysInsIvkValApiService.add(sysInsIvkValDTO);
					sysInsIvkValDTO.setId(sysInsIvkValId);
				}
				else {
					sysInsIvkValApiService.modify(sysInsIvkValDTO);
				}

//				success = true;
				MessageDTO<String> messageDTO = spdbankApi.getVerifyCode(fundRasInsId, prjId);
				if(null == messageDTO || !messageDTO.isSuccess()) {
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("获取验证码提交失败");
				}
				if(null != messageDTO) {
					respCode = messageDTO.getCode();
					respDesc = messageDTO.getMessage();
					success = messageDTO.isSuccess();
				}
				if(success) {
					respCode = SysInsInterfaceConstant.DEFAULT_RESULT_CODE_SUCC;
					data.put("getVerifyCodeSucc", "true");
				}
			}
		}
		catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "getVerifyCode", "获取验证码提交失败" + ex.getMessage());
			ex.printStackTrace();
		}
		catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取验证码提交失败!");
			BaseLogUtils.error(logger, "getVerifyCode", "获取验证码提交失败" + ex.getMessage());
			ex.printStackTrace();
		}
		finally {

			// 更新调用结果
			try {
				if(null != sysInsIvkValDTO && null != sysInsIvkValDTO.getId() && !sysInsIvkValDTO.isSuccess()) {
					if(StringUtils.isNotEmpty(respCode)) {
						sysInsIvkValDTO.setRespCode(respCode);
					}
					else {
						if(success) {
							sysInsIvkValDTO.setRespCode(SysInsInterfaceConstant.DEFAULT_RESULT_CODE_SUCC);
						}
						else {
							sysInsIvkValDTO.setRespCode(SysInsInterfaceConstant.DEFAULT_RESULT_CODE_FAILED);
						}
					}
					if(StringUtils.isNotEmpty(respDesc)) {
						sysInsIvkValDTO.setRespDesc(respDesc);
					}
					else {
						if(success) {
							sysInsIvkValDTO.setRespDesc(SysInsInterfaceConstant.DEFAULT_RESULT_DESC_SUCC);
						}
						else {
							sysInsIvkValDTO.setRespDesc(SysInsInterfaceConstant.DEFAULT_RESULT_DESC_FAILED);
						}
					}
					sysInsIvkValApiService.modify(sysInsIvkValDTO);
			    	BaseLogUtils.info(logger, "getVerifyCode", "sysInsIvkValApiService.modify result >>>>>> "
			    			+ "" + JSONObject.toJSONString(sysInsIvkValDTO));
				}
			}
			catch(Exception ex) {
				BaseLogUtils.error(logger, "getVerifyCode", "获取验证码提交.保存调用记录异常" + ex.getMessage());
			}
			if(!success) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("获取验证码提交失败" + (StringUtils.isNotEmpty(respDesc) ? "，"+respDesc : "!"));
			}
		}

		return result;
	}

	/**
	 * 提交用户输入的验证码进行开户
	 * @param prjId 项目ID
	 * @param verifyCode 用户输入的验证码
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	@RequestMapping(value = "/createAccount.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object createAccount(Long prjId, String verifyCode, Long fundRasInsId, Long cstId,
			HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "开户成功!");

		SysInsIvkValDTO sysInsIvkValDTO = null; // 准入校验接口调用结果
		boolean success = false;
		String respCode = null;
		String respDesc = null;
		try {
			sysInsIvkValDTO = sysInsIvkValApiService.getSysInsIvkValDTO(prjId, fundRasInsId,
					SysInsInterfaceConstant.InsTypeIntfConstant.SPDBANK_TYPE_CREATE_ACCOUNT);
			if(null == sysInsIvkValDTO) {
				sysInsIvkValDTO = new SysInsIvkValDTO();
			}
			success = sysInsIvkValDTO != null && sysInsIvkValDTO.isSuccess();
			if(!success) { // 如果之前是不成功状态，那么重新调用接口
				sysInsIvkValDTO.setPrjId(prjId);
				sysInsIvkValDTO.setCstId(cstId);
				sysInsIvkValDTO.setInsId(fundRasInsId);
				sysInsIvkValDTO.setTypeCd(SysInsInterfaceConstant.InsTypeIntfConstant.SPDBANK_TYPE_CREATE_ACCOUNT);
				if(null == sysInsIvkValDTO.getId()) {
					Long sysInsIvkValId = sysInsIvkValApiService.add(sysInsIvkValDTO);
					sysInsIvkValDTO.setId(sysInsIvkValId);
				}
				else {
					sysInsIvkValApiService.modify(sysInsIvkValDTO);
				}

				MessageDTO<String> messageDTO = spdbankApi.createAccount(fundRasInsId, prjId, verifyCode);
				result.setData(messageDTO);
				if(null == messageDTO || !messageDTO.isSuccess()) {
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("验证码无效或超时!");
				}
				if(null != messageDTO) {
					respCode = messageDTO.getCode();
					respDesc = messageDTO.getMessage();
					success = messageDTO.isSuccess();
					if(messageDTO.isSuccess()) {
						respCode = SysInsInterfaceConstant.DEFAULT_RESULT_CODE_SUCC;
					}
				}

			}
		}
		catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("验证码无效或超时!");
			BaseLogUtils.error(logger, "createAccount", "开户失败"+ ex.getMessage());
			ex.printStackTrace();
		}
		catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("验证码无效或超时!");
			BaseLogUtils.error(logger, "createAccount", "开户失败"+ ex.getMessage());
			ex.printStackTrace();
		}
		finally {

			// 更新调用结果
			try {
				if(null != sysInsIvkValDTO && null != sysInsIvkValDTO.getId() && !sysInsIvkValDTO.isSuccess()) {
					if(StringUtils.isNotEmpty(respCode)) {
						sysInsIvkValDTO.setRespCode(respCode);
					}
					else {
						if(success) {
							sysInsIvkValDTO.setRespCode(SysInsInterfaceConstant.DEFAULT_RESULT_CODE_SUCC);
						}
						else {
							sysInsIvkValDTO.setRespCode(SysInsInterfaceConstant.DEFAULT_RESULT_CODE_FAILED);
						}
					}
					if(StringUtils.isNotEmpty(respDesc)) {
						sysInsIvkValDTO.setRespDesc(respDesc);
					}
					else {
						if(success) {
							sysInsIvkValDTO.setRespDesc(SysInsInterfaceConstant.DEFAULT_RESULT_DESC_SUCC);
						}
						else {
							sysInsIvkValDTO.setRespDesc(SysInsInterfaceConstant.DEFAULT_RESULT_DESC_FAILED);
						}
					}
					sysInsIvkValApiService.modify(sysInsIvkValDTO);
			    	BaseLogUtils.info(logger, "getVerifyCode", "sysInsIvkValApiService.modify result >>>>>> "
			    			+ "" + JSONObject.toJSONString(sysInsIvkValDTO));
				}
			}
			catch (Exception ex) {
				BaseLogUtils.error(logger, "createAccount", "开户.保存调用记录异常" + ex.getMessage());
			}
			if(!success) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("验证码无效或超时" + (StringUtils.isNotEmpty(respDesc) ? "，"+respDesc : "!"));
			}
		}
		return result;
	}

	/**
	 * REQ-7008 pc端浦发只修改月收入
	 * @methodName saveInfo
	 * @author: liuqihao@lionbridgecapital.cn
	 * @createDate: 2019-12-05 17:44
	 * @version: V1.0.0
	 * @param prjId
	 * @param request
	 * @param response
	 * @Return java.lang.Object
	 * @updateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value = "/saveInfo.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object saveInfo(Long prjId, HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "保存成功!");
		String monIncmYuan = request.getParameter("monIncmYuan");
		String highDgCd = request.getParameter("highDgCd");
		if (StringUtils.isBlank(monIncmYuan)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("请输入月收入!");
			return result;
		}
		if (StringUtils.isBlank(highDgCd)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("请维护学历!");
			return result;
		}
		try {
			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(prjId);
			BaseLogUtils.info(logger, "saveInfo", "项目浦发信息保存 ： prjBscInfoVO = " + JSONObject.toJSONString(prjBscInfoVO));

			// 保存月收入
			CstNpBscInfoVO cstNpBscInfoVO = this.cstNpBscInfoService.selectById(prjBscInfoVO.getCstId());
			if (null != cstNpBscInfoVO) {
				boolean flag = false;
				// 月收入转为分
				Long mrIncMoney = DecimalUtils.convertYuanToFen(Double.valueOf(monIncmYuan));
				// 月收入转换年收入
				Double yrInc = DecimalUtils.mul(mrIncMoney.doubleValue(), 12d);
				if (!Objects.equals(cstNpBscInfoVO.getYrInc().doubleValue(), yrInc)) {
					cstNpBscInfoVO.setYrInc(yrInc.longValue());
					flag = true;
				}
				//学历
				if (!Objects.equals(cstNpBscInfoVO.getHighDgCd(), highDgCd)) {
					cstNpBscInfoVO.setHighDgCd(highDgCd);
					flag = true;
				}
				if (flag) {
					cstNpBscInfoVO.setMdfTm(new Date());
					cstNpBscInfoVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
					BaseLogUtils.info(logger, "saveInfo", "修改自然人信息 cstNpBscInfoVO" + JSON.toJSONString(cstNpBscInfoVO), CurrentThreadContext.getCurrentUserName());
					boolean modify = this.cstNpBscInfoService.modify(cstNpBscInfoVO);
					BaseLogUtils.info(logger, "saveInfo", "修改自然人信息modify=" + modify, CurrentThreadContext.getCurrentUserName());
				}
			}
		}
		catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "saveInfo", "项目浦发信息保存失败 " + ex.getMessage());
			ex.printStackTrace();
		}
		catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("保存失败!");
			BaseLogUtils.error(logger, "saveInfo", "项目浦发信息保存失败 " + ex.getMessage());
			ex.printStackTrace();
		}

		return result;
	}


}
